<?php


   class UserAction extends CommonAction{
   
      public function tixian(){
	  
	   if(isset($_GET['time'])){
	      
		  $this->day=$_GET['time'];
	   
	      $s=strtotime($_GET['time']);
	      $e=$s+86400;
	      $w['time']=array(array('gt',$s),array('lt',$e),'and');
		  
		  
		  
		  
		  	
		//搜索日成功提现
		
		$today=strtotime(date(Y-m-d));
		$ws['status']=array('eq','1');
		$ws['time']=array(array('gt',$s),array('lt',$e),'and');
		$cg=M('tixian')->where($ws)->select();
		
		$schenggong=0;
		foreach($cg as $v){
		
		  $schenggong+=$v['money'];
		
		}
		$this->schenggong=$schenggong;
		
		
		
		//搜索日失败
		
		$ws['status']=array('neq','1');
		
		$cg=M('tixian')->where($ws)->select();
		
		$sshibai=0;
		foreach($cg as $v){
		
		  $sshibai+=$v['money'];
		
		}
		$this->sshibai=$sshibai;
	   
	   
	   }
	  
	  
        $re=$this->page('tixian',10,'id desc',$w);
		
		$i=-1;
		foreach($re as $v){
		  $i++;
		  
		  $user=M('dailiuser')->where('id="%s"',$v['tixianid'])->find();
		  
		  $re[$i]['username']=$user['username'];
		
		}
		
		$this->re=$re;
		
		
		//今日成功提现
		
		$today=strtotime(date(Ymd));
		//dump($today);
		$where['status']=array('eq','1');
		$where['time']=array('gt',$today);
		$cg=M('tixian')->where($where)->select();
		
		$chenggong=0;
		foreach($cg as $v){
		
		  $chenggong+=$v['money'];
		
		}
		$this->chenggong=$chenggong;
		
		
		
		//今日失败
		
		$where['status']=array('neq','1');
		
		$cg=M('tixian')->where($where)->select();
		
		$shibai=0;
		foreach($cg as $v){
		
		  $shibai+=$v['money'];
		
		}
		$this->shibai=$shibai;
		
		
		
		
		
		
	    $this->display();
	  
	  }
	  
	  
	  public function jujue(){
	  
	    $arr['status']=3;
		
		$re=M('tixian')->where('id="%s"',$_GET['id'])->save($arr);
		
		if($re!==false){
		
		  $this->success('状态修改成功');exit();
		
		}
	  
	  }
	  
	  
	  public function shibai(){
	  
	 
        $re=$this->page('tixian',10,'id desc','status!=1');
		
		$i=-1;
		foreach($re as $v){
		  $i++;
		  
		  $user=M('dailiuser')->where('id="%s"',$v['tixianid'])->find();
		  
		  $re[$i]['username']=$user['username'];
		
		}
		
		$this->re=$re;
		
		
		//今日成功提现
		
		$today=strtotime(date(Y-m-d));
		$where['status']=array('eq','1');
		$where['time']=array('gt',$today);
		$cg=M('tixian')->where($where)->select();
		
		$chenggong=0;
		foreach($cg as $v){
		
		  $chenggong+=$v['money'];
		
		}
		$this->chenggong=$chenggong;
		
		
		
		//今日失败
		
		$where['status']=array('neq','1');
		
		$cg=M('tixian')->where($where)->select();
		
		$shibai=0;
		foreach($cg as $v){
		
		  $shibai+=$v['money'];
		
		}
		$this->shibai=$shibai;
		
		
		
		
	    $this->display('tixian');
	  }
	  
	  
	  public function search(){
	  
	    $start= strtotime($_GET['time']);
		$end=$start+86400;
		$where['type']=array('eq','1');
	    $where['status']=array('eq','1');
		$where['time']=array(array('gt',$start),array('lt',$end),'and');
		$cg=M('buyer')->where($where)->select();
		//last('buyer');
		//成功
		$chenggong=0;
		foreach($cg as $v){
		
		   $chenggong+=$v['money'];
		
		}
		$this->chenggong=$chenggong;
		
		//失败
		
		$where['status']=array('eq','2');
		$cg=M('buyer')->where($where)->select();
			$shibai=0;
		foreach($cg as $v){
		
		   $shibai+=$v['money'];
		
		}
		$this->shibai=$shibai;
		
		
		//审核中
		
		$where['status']=array('eq','0');
		$cg=M('buyer')->where($where)->select();
			$shenhezhong=0;
		foreach($cg as $v){
		
		   $shenhezhong+=$v['money'];
		
		}
		$this->shenhezhong=$shenhezhong;
		
		
		
	    $this->time=$_GET['time'];
		
		$w['type']=array('eq','1');
	   
		$w['time']=array(array('gt',$start),array('lt',$end),'and');
		
        $this->listpage('buyer',10,'id desc',$w);
	    $this->display();
	  
	  }
	  
	  
	  
	  
	  public function agree(){
	  
	     $iszhifu=M('tixian')->where('id='.$_GET['id'])->find();
		 
		 if($iszhifu['status']!=1){
		 
		 
		   $data['status'] = '1';
          M('tixian')->where('id='.$_GET['id'])->save($data);
		  
		  $this->success('修改成功');
		  exit();
		 
	  	 //引入hongbao api
        vendor('Wxhongbao.hongbao');
        $tixian = M('tixian')->where('id='.$_GET['id'])->find();
	  	$user = M('user')->where("openid='".$tixian['openid']."'")->find();
		
	  	$wx = M('wx')->where('id=2')->find();
	  	header("Content-type: text/html; charset=utf-8");

		//APPID
		define('APPID',$wx['appid']);
		//商户id，10025915
		define('MCHID',$wx['shanghuhao']);
		//微信支付密钥，商户平台中，账户设置->安全设置->API安全->API密钥：api设置
		define('PARTNERKEY',$wx['miyao']);

		//以下接口均已调试成功，需要你自行对接到自己的系统中。
        //dump($tixian['money']);die;
		
		$shi=$tixian['money']-$tixian['maolirun'];
		
		$shi=round($shi,2);

		//企业付款
		$obj1 = array();
		$obj1['openid']         	= $tixian['openid'];
		$obj1['amount']         	= $shi*100;
		$obj1['desc']        		= "现金提现付款";
		$obj1['mch_appid']         	= APPID;
		$obj1['mchid']         		= MCHID;
		$obj1['partner_trade_no']	= MCHID.date('YmdHis').rand(1000, 9999);
		$obj1['spbill_create_ip']   = $_SERVER['REMOTE_ADDR'];
		$obj1['check_name']      	= "NO_CHECK";
		

		$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
		$wxHongBaoHelper = new WxPay();
		$result = $wxHongBaoHelper->pay($url, $obj1);
	    $obj =  simplexml_load_string($result, null, LIBXML_NOCDATA); //转换成对象
	   // dump($obj);die;
        $arr  = $obj->result_code;//提取对象中某个属性值
        if($arr=="SUCCESS"){
          $data['status'] = '1';
          M('tixian')->where('id='.$_GET['id'])->save($data);
          $this->success("发放成功！");
        }else{
		   $data['status'] = '2';
          M('tixian')->where('id='.$_GET['id'])->save($data);
        	$this->error("发送失败！");
        }
		
		}
	  }
	  public function refuse(){
	  	  $data['status'] = '2';

          $re = M('buyer')->where('id='.$_GET['id'])->save($data);
          $tixian = M('buyer')->where('id='.$_GET['id'])->find();
         // $user = M('user')->where('id='.$tixian['openid'])->find();
          //$data1['money'] = $user['money']+$tixian['money'];
         // $re = M('user')->where("openid='".$tixian['openid']."'")->save($data1);
          if($re!==false){
          $this->success("审核已经拒绝！");
          }
	  }
	  
	  
	  public function xiangxi(){
	  
	    $order=M('buyer')->where('id="%s"',$_GET['id'])->find();
		
		$openid=$order['openid'];
		
		$user=M('user')->where('openid="%s"',$openid)->find();
		$this->yue=$user['money'];
	
		$moreorder=M('orderinfo')->where('openid="%s"',$openid)->select();
	  
	    $arr=array();
		
		foreach($moreorder as $v){
		
		   $arr[]=$v['id'];
		
		}
		
	
		
		$where['type']=array('neq','1');
		$where['pid']=array('IN',$arr);
		$where['status']=array('eq','1');
		$buyer=M('buyer')->where($where)->select();
		
		$shouyi=0;
		foreach($buyer as $v){
		
		   $shouyi+=$v['newmoney'];
		}
		
		
		$dashang=M('buyer')->where('type!=1 and openid="%s" and status=1',$openid)->select();
		
		$ds=0;
		foreach($dashang as $v){
		 
		 $ds+=$v['money'];
		
		}
		
		$this->dashang=$ds;
		
		
		$this->shouyi=$shouyi;
		
	   $this->listpage('buyer',10,'id desc',$where);
		
	  
	     $this->display();
	  
	  }
	  
	  
	  
	  public function se(){
	  
	      $this->start=$_GET['start'];
		  $this->end=$_GET['end'];
		 
		   $start=strtotime($_GET['start']);
		   
		   $end=strtotime($_GET['end']);
		   
		   
		   if($start>$end){
		   
		     $this->error('开始时间不能大于结束时间');
		   
		   }
		   
		   
		   
		 $end=$end+86400;
		   
		// $where['status']=array('eq','1');
		$where['time']=array(array('gt',$start),array('lt',$end),'and');
		
		
	    $re=$this->page('tixian',10,'id desc',$where);
		
		$i=-1;
		
		foreach($re as $v){
		 
		 $i++;
		 
		 $user=M('dailiuser')->where('id="%s"',$v['tixianid'])->find();
		 
		 
		 
		 $re[$i]['username']=$user['username'];
		 
		
		
		}
		
		
		$this->re=$re;
		
		   
		 //  dump(M('buyer')->getLastSql());
		$where['status']=array('eq','1');
		$cg=M('tixian')->where($where)->select();
		
		//dump(M('tixian')->getlastsql());
		
		//成功
		$chenggong=0;
		foreach($cg as $v){
		
		   $chenggong+=$v['money'];
		
		}
		$this->chenggong=$chenggong;
		
		//失败
		
		$where['status']=array('eq','2');
		$cg=M('tixian')->where($where)->select();
			$shibai=0;
		foreach($cg as $v){
		
		   $shibai+=$v['money'];
		
		}
		$this->shibai=$shibai;
		
		
		
		
		
	  
     
	   
	  
	    $this->display();
	  
	  }
   
   
   }